﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using System.IO;

public partial class en_ucControl_ucUpload : System.Web.UI.UserControl
{
    string _fileName = "";
    string _IdImage = "";
    TblImagesDAO imageDAO = new TblImagesDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void CheckAlbum()
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //bool bFlag = false;

        //try
        //{

        //    TblImages entity = new TblImages();
        //    string path = Server.MapPath("~\\Admin\\images\\Items");
        //    // string filename = fileUpload.FileName;

        //    // Get the name of the file to upload.
        //    _fileName = Server.HtmlEncode(fileUpload.FileName);

        //    if (File.Exists(path + "\\" + _fileName))
        //    {
        //        lb_thongbao.Text = "The FileName is already existed.";
        //    }

        //    else
        //    {
        //        try
        //        {
        //            int fileSize = fileUpload.PostedFile.ContentLength;
        //            //File size
        //            if (fileSize > 1050000)
        //            {
        //                lb_thongbao.Text = "Your file was not uploaded because it exceeds the 1 MB size limit.";
        //            }

        //            // Get the extension of the uploaded file.
        //            string extension = System.IO.Path.GetExtension(_fileName);
        //            if (extension != ".jpg" && extension != ".bmp")
        //            {
        //                lb_thongbao.Text = "Your file was not uploaded because it does not have picture extension";
        //            }

        //            //Insert
        //            string ids = Request.QueryString["id"];
        //            if (ids == null || ids == "")
        //            {
                        
        //                //entity.ImagePath = textPath.Text;

        //                fileUpload.SaveAs(Server.MapPath("iamges\\items\\" + _fileName));

        //                entity.ImageURL = _fileName;
        //                //imageDAO.InsUpdImages(entity, 1);
        //            }
        //            else
        //            {
        //                //Update
        //                int id = Global.ConvertToInt(Request.QueryString["id"].ToString());
        //                //TblImageFooter img = imageDAO.Get(id);
        //                if (img != null)
        //                {
        //                    string fname = img.ImageURL;
        //                    if (fname != null)
        //                    {
        //                        File.Delete(Server.MapPath("ImageFooter\\" + fname));
        //                    }
        //                    // Update moi
        //                    entity.IdImage = id;
        //                    entity.ImagePath = textPath.Text;
        //                    entity.ImageURL = _fileName;
        //                    fileUpload.SaveAs(Server.MapPath("ImageFooter\\" + _fileName));

        //                    imageDAO.Update(entity, TblImageFooterDAO.UPDATE_MODE.UPDATE);
        //                }

        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            Session[GlobalWeb.SESSION_ERROR] = ex;
        //            Response.Redirect(GlobalWeb.ERROR_PAGE, true);
        //        }
        //        bFlag = true;
        //    }
        //}
        //catch (Exception ex)
        //{
        //    Session[GlobalWeb.SESSION_ERROR] = ex;
        //    Response.Redirect(GlobalWeb.ERROR_PAGE, true);
        //}
        //if (bFlag)
        //    Response.Redirect("~/Admin/ImageFooter.aspx", true);
        Response.Redirect("~/en/Default.aspx");
    }
}
